package com.example.administrator.testz.News;/**
 * Created by Administrator on 2016/3/11.
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.testz.R;

import java.util.List;

import pri.zey.custom.tools.image.TImageLoaderAsyncTask;


/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class AdapterNews extends BaseAdapter implements AbsListView.OnScrollListener{

    private List<NewsBean> mList;
    private LayoutInflater mInfalter;
    private TImageLoaderAsyncTask mImageLoader;
//    private u_ImageLoaderThread mImageLoader;
    private int start, end;
    private String[] URLS;
    private boolean isFirstIn = true;

    public AdapterNews(Context mContext, List<NewsBean> mList, ListView listView) {
        this.mList = mList;
        this.mInfalter = LayoutInflater.from(mContext);
        this.mImageLoader = new TImageLoaderAsyncTask(listView, mList);
        listView.setOnScrollListener(this);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder = null;
        if(convertView == null){
            mViewHolder = new ViewHolder();
            convertView = mInfalter.inflate(R.layout.z_row_news, null);
            mViewHolder.imageView1 = (ImageView) convertView.findViewById(R.id.imageView1);
            mViewHolder.titleView = (TextView) convertView.findViewById(R.id.titleView);
            mViewHolder.contentView = (TextView) convertView.findViewById(R.id.contentView);
            convertView.setTag(mViewHolder);
        }else{
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        mViewHolder.imageView1.setImageResource(R.mipmap.ic_launcher);
        String url = mList.get(position).getmPhotoURL();
        mViewHolder.imageView1.setTag(url);
//        new TImageLoaderAsyncTask().showImageByThread(mViewHolder.imageView1, url);
//        new TImageLoaderAsyncTask().showImageByAscynTask(mViewHolder.imageView1, url);
//        mImageLoader.showImageByAscynTask(mViewHolder.imageView1, url);
        mViewHolder.titleView.setText(mList.get(position).getmTitle());
        mViewHolder.contentView.setText(mList.get(position).getmContent());
        return convertView;
    }

    //滚动过程中变动触发
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if(scrollState == SCROLL_STATE_IDLE){
            //加载可见项
            mImageLoader.loadImages(start, end);
        }else{
            //停止任务
            mImageLoader.cancelAllTask();
        }
    }

    //滑动过程中触发（包括第一次加载）
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.start = firstVisibleItem;
        this.end = firstVisibleItem + visibleItemCount;
        //第一次显示时候使用
        //visibileItemCount > 0条件为了跳过一开始多次加载
        if(isFirstIn && visibleItemCount > 0){
            mImageLoader.loadImages(start, end);
            isFirstIn = false;
        }
    }

    class ViewHolder{
        public TextView titleView, contentView;
        public ImageView imageView1;
    }
}
